package cn.edu.nuc.film.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.entity.Film;
import cn.edu.nuc.film.entity.FilmCity;
import cn.edu.nuc.film.service.interfaces.FilmCityService;

@Controller
@RequestMapping(value = "/film/filmcity")
public class FilmCityController {

	@Autowired
	private FilmCityService fcService;
	
	/**
	 * 影城所属影片
	 * @param fcId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/filmlist",method=RequestMethod.GET)
	public String fcFilmList(int fcId, String sname, HttpServletRequest request) {

		// 获取要查询的影片名字
		// String sname = request.getParameter("sname");
		if (sname == null || sname.equals("")) {
			sname = null;
		}
		/* System.out.println("sname--" + sname); */
		// 设置分页信息
		PageControl pc = new PageControl();

		// 设置初始当前页currindex = 1
		Integer currindex = 1;

		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if (request.getParameter("index") != null) {
			currindex = Integer.parseInt(request.getParameter("index"));
		}

		// 将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);
		Integer fcid = 0;
		//获取影城信息，传到前台页面，显示
		FilmCity filmCity = null;
		if( fcId != 0){
			fcid = fcId;
			filmCity = fcService.detail(fcid);
		}
		// 调用service中的分页方法，获取分页信息
		Pager<Film> pager = fcService.fcFilmList(pc, sname,fcid);
		//获取影城信息，这里主要是获取影城名
		
		// 将分页信息传送到前台
		request.setAttribute("pager", pager);
		request.setAttribute("filmCity", filmCity);
		// 返回filmlist页面
		return "sys/filmcity/filmlist";

	}
	
	
	/**
	 * 影城列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/fcList", method = RequestMethod.GET)
	public String fcList(String sname, HttpServletRequest request) {

		// 获取要查询的名字
		// String sname = request.getParameter("sname");
		if (sname == null || sname.equals("")) {
			sname = null;
		}
		// 设置分页信息
		PageControl pc = new PageControl();

		// 设置初始当前页currindex = 1
		Integer currindex = 1;

		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if (request.getParameter("index") != null) {
			currindex = Integer.parseInt(request.getParameter("index"));
		}

		// 将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);

		// 调用service中的分页方法，获取分页信息
		Pager<FilmCity> pager = fcService.list(pc, sname);
		// 将分页信息传送到前台
		request.setAttribute("pager", pager);
		// 将获取的分页信息返回到jsp界面
		return "sys/filmcity/list";
	}

	/**
	 * 进入添加影城界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toaddFc", method = RequestMethod.GET)
	public String toaddFc() {
		// 进入添加影城的jsp界面
		return "sys/filmcity/add";
	}

	/**
	 * 新增影城
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addFc", method = RequestMethod.POST)
	public String addFc(FilmCity fc) {
		// 执行添加操作
		fcService.insert(fc);
		// 重定向到fcList
		return "redirect:fcList";

	}

	/**
	 * 进入修改界面
	 * 
	 * @param fcid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toeditFc", method = RequestMethod.GET)
	public String toeditFc(int fcId, Model model) {
		// 获取影城信息，以进行编辑
		FilmCity fc = fcService.detail(fcId);
		// 将获取的影城信息传到前台界面
		model.addAttribute("fc", fc);
		return "sys/filmcity/edit";

	}

	/**
	 * 执行编辑操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "/editFc", method = RequestMethod.POST)
	public String editFc(FilmCity fc) {
		// 执行编辑操作
		fcService.updateFc(fc);
		return "redirect:fcList";

	}

	/**
	 * 删除影城
	 * 
	 * @param fcid
	 * @return
	 */
	@RequestMapping(value = "/deleteFc", method = RequestMethod.GET)
	public String deleteFc(int fcId) {
		fcService.deleteFc(fcId);
		return "redirect:fcList";

	}

}
